package com.dianxin.secure.persist;
import org.apache.commons.pool.impl.GenericObjectPool.Config;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public  class RedisStore {
	protected JedisPool _jedisPool;
	protected String _server = "localhost";
	protected int _port = 6379;
	protected String _password;
	
	protected Jedis borrowJedis() {
		return _jedisPool.getResource();
	}

	protected void returnJedis(Jedis jedis) {
		if (jedis != null)
			_jedisPool.returnResource(jedis);
	}

	
	public RedisStore() {
		_jedisPool = new JedisPool(_server, _port);
	}
	/**
	 * @param config redis.clients.jedis.JedisPoolConfig
	 * @param host
	 * @param port
	 * @param timeout SO_TIMEOUT 超时时间
	 * @param password
	 */	
	public RedisStore(Config config, String host, int port, int timeout, final String password) {
		if (config == null) config = new JedisPoolConfig();
		_password = "".equals(password) ? null : password;
		_jedisPool = new JedisPool(config, host, port, timeout, _password);
		_server = host;
		_port = port;
	}
	
	public String getServer() {
		return _server;
	}

	public void setServer(String server) {
		_server = server;
	}

	public int getPort() {
		return _port;
	}

	public void setPort(int port) {
		_port = port;
	}

	public String getPassword() {
		return _password;
	}

	public void setPassword(String password) {
		_password = password;
	}
	
	

	protected void returnBrokenJedis(Jedis jedis) {
		if (jedis != null)
			_jedisPool.returnBrokenResource(jedis);
	}	
	
	
	
	
}
